<?php

namespace app\common\model\mysql;

use think\Model;

class SysRoleDept extends Model
{
    //根据角色ID查询部门下拉树结构
    public function getRoleDeptIdArray($roleId){
        if(empty($roleId)){
            return false;
        }
        $where = [
            "role_id" =>$roleId,
        ];
        $result = $this->where($where)->column('dept_id');
        return $result;
    }

    //根据角色Ids查询部门id数组
    public function getRoleDeptIdsArray($roleIds){
        if(empty($roleIds)){
            return false;
        }
        $where = [
            ['role_id','in',$roleIds]
        ];
        $result = $this->where($where)->column('dept_id');
        return $result;
    }

    //删除角色与部门关联表数据
    public function deleteAllRoleDept($roleId){
        if(empty($roleId)){
            return false;
        }
        $where = [
            "role_id" =>$roleId,
        ];
        return $this->where($where)->delete();
    }

    //批量插入角色与部门关联表数据
    public function insertAllRoleDept($deptIds,$roleId){
        $data = [];
        foreach ($deptIds as $deptId) {
            $data[] = [
                'role_id' => $roleId,
                'dept_id' => $deptId,
            ];
        }
        return $this->insertAll($data);
    }
}
